## ECC2 / Example 1.1.4: A binary code (8,20,3)

from PyM import *

''' 
The PyECC functions used in this example are 
hd (the Hamming distance) and cyclic_shifts, 
which returns the list of the cyclic
permutations of a list or vector. 
In this context, the function
cyclic_shift is auxiliary and yields 
the cylic permutation of a list or vector
the places its first element at the end
'''

def hd(x,y):
    d = 0
    l = len(x)
    if len(y) != l: 
        return 'hd Error: the lengths have to be equal'
    for j in range(l):
        if x[j] != y[j]:
            d += 1
    return d

def cyclic_shift(x): 
    if isinstance(x,Vector_type):
        x = list(x)
    return [x[-1]] + x[:-1]

def cyclic_shifts(x):
    X = [x]
    if isinstance(x,Vector_type):
        x = list(x)
    y = cyclic_shift(x)
    while y != x:
        X = X + [y]
        y = cyclic_shift(y)
    return list(X)
    
a = [1,1,0,1,0,0,0,0]; X = cyclic_shifts(a)
b = [1,1,1,0,0,1,0,0]; Y = cyclic_shifts(b)
c = [1,0,1,0,1,0,1,0]; Z = cyclic_shifts(c)

Dax = [hd(a,x) for x in X[1:]]
Day = [hd(a,y) for y in Y]
Daz = [hd(a,z) for z in Z]
Dby = [hd(b,y) for y in Y[1:]]
Dbz = [hd(b,z) for z in Z]
Dcz = [hd(c,z) for z in Z[1:]]

show("dC =",min(Dax+Day+Daz+Dby+Dbz+Dcz))

